﻿// Dynamic tree editor - tree-like data structures editor for .NET/Mono.
// Copyright (C) 2006  Vladimir Sibirov
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Threading;
using Microsoft.Win32;

// TODO: Russian localization

namespace TreeEditor
{	
	/// <summary>
	/// Main application window class.
	/// </summary>
	public partial class MainWindow
	{
		/// <summary>
		/// Culture info.
		/// </summary>
		private string culture;
		
		/// <summary>
		/// Main window constructor
		/// </summary>
		public MainWindow()
		{
			// Internationalization support:
			// Trying to load preferences from registry
			RegistryKey regKey = Registry.CurrentUser.OpenSubKey("Software\\DynamicTreeEditor");
			if(regKey != null)
			{
				culture = (string) regKey.GetValue("Culture");
				Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
				regKey.Close();
			}
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.EnableVisualStyles();
			Application.Run(new MainWindow());
		}
		
		/// <summary>
		/// Culture information.
		/// </summary>
		internal string Culture
		{
			get
			{
				return Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
			}
		}
		
		/// <summary>
		/// Manipulates with status message.
		/// </summary>
		internal string StatusMessage
		{
			get
			{
				return this.mainStatusLabel.Text;
			}
			set
			{
				this.mainStatusLabel.Text = value;
			}
		}
		
		/// <summary>
		/// Menu New click handler.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void NewToolStripMenuItemClick(object sender, System.EventArgs e)
		{
			// Creating a new child form
			DocForm doc = new DocForm();
			doc.MdiParent = this;
			doc.Culture = this.Culture;
			doc.NewDoc();
			doc.Show();
		}
		
		/// <summary>
		/// Menu Close click handler.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void CloseToolStripMenuItemClick(object sender, System.EventArgs e)
		{
			((DocForm) this.ActiveMdiChild).CloseDoc();
		}
		
		/// <summary>
		/// Quits the application.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void ExitToolStripMenuItemClick(object sender, System.EventArgs e)
		{
			// Pass the entire proccess into the close handler.
			this.Close();
		}
		
		/// <summary>
		/// Opens new document.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void OpenToolStripMenuItemClick(object sender, System.EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog();
			ofd.Filter = (culture == "ru") ? "XML документы деревьев (*.tree)|*.tree" : "Tree XML documents (*.tree)|*.tree";
			ofd.CheckFileExists = true;
			ofd.ShowDialog();
			if(ofd.FileName != "" && ofd.FileName != null)
			{
				DocForm doc = new DocForm();
				doc.MdiParent = this;
				doc.Culture = this.Culture;
				doc.OpenDoc(ofd.FileName);
				doc.Show();
			}
		}
		
		/// <summary>
		/// Saves the document.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void SaveToolStripMenuItemClick(object sender, System.EventArgs e)
		{
			((DocForm) this.ActiveMdiChild).Save();
		}
		
		/// <summary>
		/// Shows the Save dialog and saves the document.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void SaveAsToolStripMenuItemClick(object sender, System.EventArgs e)
		{
			((DocForm) this.ActiveMdiChild).SaveAs();
		}
		
		/// <summary>
		/// Shows About dialog.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void AboutToolStripMenuItemClick(object sender, System.EventArgs e)
		{
			AboutDialog ad = new AboutDialog();
			ad.Culture = this.Culture;
			ad.ShowDialog();
		}
		
		/// <summary>
		/// Shows preferences.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void PreferencesToolStripMenuItemClick(object sender, System.EventArgs e)
		{
			PrefsDialog pd = new PrefsDialog();
			pd.ShowDialog();
			if(pd.DialogResult == DialogResult.OK)
			{
				if(pd.Culture == "default")
				{
					Registry.CurrentUser.DeleteSubKey("Software\\DynamicTreeEditor", false);
				}
				else
				{
					culture = pd.Culture;
					Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
					RegistryKey regKey = Registry.CurrentUser.CreateSubKey("Software\\DynamicTreeEditor");
					regKey.SetValue("Culture", culture);
					regKey.Close();
				}
			}
		}
		
		/// <summary>
		/// Clears the document.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void ClearToolStripMenuItemClick(object sender, System.EventArgs e)
		{
			TreeClearDialog askClear = new TreeClearDialog();
			askClear.ShowDialog();
			if(askClear.DialogResult == DialogResult.OK)
			{
				((DocForm) this.ActiveMdiChild).ClearDoc();
			}
		}
		
		/// <summary>
		/// Shows help.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void HelpToolStripMenuItem1Click(object sender, System.EventArgs e)
		{
			HelpForm hf = new HelpForm();
			hf.Show();
		}		
		
		/// <summary>
		/// Disables inactive items.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void FileToolStripMenuItemDropDownOpening(object sender, System.EventArgs e)
		{
			if(this.ActiveMdiChild == null)
			{
				closeToolStripMenuItem.Enabled = false;
				saveToolStripMenuItem.Enabled = false;
				saveAsToolStripMenuItem.Enabled = false;
			}
			else
			{
				closeToolStripMenuItem.Enabled = true;
				saveToolStripMenuItem.Enabled = true;
				saveAsToolStripMenuItem.Enabled = true;
			}
		}
		
		/// <summary>
		/// Disables inactive items.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void EditToolStripMenuItemDropDownOpening(object sender, System.EventArgs e)
		{
			if(this.ActiveMdiChild == null)
				clearToolStripMenuItem.Enabled = false;
			else
				clearToolStripMenuItem.Enabled = true;
		}
		
		/// <summary>
		/// Auxilliary procdures.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void MainWindowLoad(object sender, System.EventArgs e)
		{
			// Auxilliary stuff here
		}
		
		/// <summary>
		/// Main window closing.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">Event arguments.</param>
		void MainWindowFormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
		{
			// First close all the child forms
			for(int i = 0; i < this.MdiChildren.Length; i++)
			{
				if(!((DocForm) this.MdiChildren[i]).CloseDoc())
				{
					// Exit has been canceled
					e.Cancel = true;
				}
			}
		}
	}
}
